Unit
	ComponentLoader;

Interface

Uses
	Classes,
	SysUtils,
	PONCompiler,
	RTTIObject;

Function LoadComponents(Const aSource : String): String;
Procedure SaveComponents(Const aDest : String; Const aComponents : TRTTIObject);

Implementation

Function LoadComponents(Const aSource : String): String;
Var
	lCompiler : TPONCompiler;
	lCtrl : Integer;
	lTemp : String;
	lTempObj : TRTTIObject;
Begin
	// Debug  WriteLn('Compiling.');
	Result := '';
	lCompiler := TPONCompiler.Create(aSource);
	lCompiler.Compile;
	// Debug  WriteLn('Creating instances at class factory.');
	For lCtrl := Low(lCompiler.Instances) To High(lCompiler.Instances) Do
	Begin
		// Debug  WriteLn(lCompiler.Instances[lCtrl].ClassName, ', ', lCompiler.Instances[lCtrl].InstanceName);
		Factory.Instance(lCompiler.Instances[lCtrl].ClassName, lCompiler.Instances[lCtrl].InstanceName);
	End;
	// Debug  WriteLn('Setting class ownerships.');
	For lCtrl := Low(lCompiler.Ownership) To High(lCompiler.Ownership) Do
	Begin
		// Debug  WriteLn(lCompiler.Ownership[lCtrl].OwnerName, ', ', lCompiler.Ownership[lCtrl].InstanceName);
		lTemp := lCompiler.Ownership[lCtrl].OwnerName;
		If lTemp = '' Then
		Begin
			// Debug  WriteLn('Rootless instance (', lCompiler.Ownership[lCtrl].InstanceName, ').');
			Factory.FindInstance(lCompiler.Ownership[lCtrl].InstanceName).Owner := Nil;
			// Debug  WriteLn('Setting ', lCompiler.Ownership[lCtrl].InstanceName, ' owner to Nil');
			Result := lCompiler.Ownership[lCtrl].InstanceName;
		End
		Else
		Begin
			// Debug  WriteLn('Instance with owner (', lCompiler.Ownership[lCtrl].InstanceName, ').');
			lTempObj :=	Factory.FindInstance(lCompiler.Ownership[lCtrl].InstanceName);
			// Debug  WriteLn('Setting ', lCompiler.Ownership[lCtrl].InstanceName, ' owner to ', lTemp);
			lTempObj.Owner := Factory.FindInstance(lTemp);
			lTempObj.Owner.Append(lTempObj);
		End;
	End;
	// Debug  WriteLn('Setting class properties.');
	For lCtrl := Low(lCompiler.Properties) To High(lCompiler.Properties) Do
	Begin
		// Debug  WriteLn(lCompiler.Properties[lCtrl].InstanceName, ',', lCompiler.Properties[lCtrl].PropertyName, ',', lCompiler.Properties[lCtrl].Value);
		Factory.FindInstance(lCompiler.Properties[lCtrl].InstanceName).Properties[lCompiler.Properties[lCtrl].PropertyName] := lCompiler.Properties[lCtrl].Value;
	End;
	lCompiler.Free;
End;

Procedure SaveComponents(Const aDest : String; Const aComponents : TRTTIObject);
Var
	lStrings : TStringList;
Begin
	lStrings := TStringList.Create;
	lStrings.Text := aComponents.AsPONString(0) + '.';
	lStrings.SaveToFile(aDest);
	lStrings.Free;
End;

End.